package ipmsg;

import ipmsg.etc.GlobalVar;
import ipmsg.gui.UtilityGui;
import ipmsg.menu.MenuSystem;
import ipmsg.network.RefreshUsers;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 程序入口
 *
 * @author idcomcn
 */
public class StartFrame extends JFrame implements RefreshUsers {
    /**
     *
     */
    private static final long serialVersionUID = -4174179696632988973L;

    private JLabel labelTips;
    private ButtonGroup buttonGroup1;
    private ButtonGroup buttonGroup2;
    private ButtonGroup buttonGroup3;
    private JList<String> guiUserList;
    private JMenu menuSystem = new MenuSystem(this);
    private JMenu menuWindows;
    private JMenu menuConfig;
    private JMenu jMenu4;
    private JMenu jMenu5;
    private JMenu jMenu6;
    private JMenuBar jMenuBar1;
    private JMenuItem jMenuItem10;
    private JMenuItem jMenuItem11;
    private JMenuItem jMenuItem12;
    private JMenuItem jMenuItem13;
    private JMenuItem jMenuItem14;
    private JMenuItem jMenuItem15;
    private JMenuItem jMenuItem16;
    private JMenuItem jMenuItem17;
    private JMenuItem jMenuItem18;

    public StartFrame() {
        System.out.println("idocmcn, start ......");
        checkVersion();
        GlobalVar.init();
        checkCharset();

        Thread th = new Thread(new ipmsg.network.MsgDaemonProcessor(this));
        th.start();
        th = new Thread(new ipmsg.network.MsgDaemonServer());
        th.start();
        System.out.println("idcomcn,xxxxxxxxxxxxxx:");
        th = new Thread(new ipmsg.network.FileDaemonServer());
        th.start();

        initTheme();
        initComponents();
        freshGui(true);

        login();
    }

    private void initComponents() {
        this.buttonGroup1 = new ButtonGroup();
        this.buttonGroup2 = new ButtonGroup();
        this.buttonGroup3 = new ButtonGroup();
        this.jPanel1 = new javax.swing.JPanel();
        this.jScrollPane1 = new javax.swing.JScrollPane();
        this.guiUserList = new JList<String>();
        this.jPanel2 = new javax.swing.JPanel();
        this.labelTips = new JLabel();
        this.jMenuBar1 = new JMenuBar();
        this.menuWindows = new JMenu();
        this.jMenuItem8 = new JMenuItem();
        this.jMenuItem9 = new JMenuItem();
        this.jMenuItem10 = new JMenuItem();
        this.jSeparator4 = new javax.swing.JSeparator();
        this.jMenuItem11 = new JMenuItem();
        this.jMenuItem12 = new JMenuItem();
        this.jMenuItem13 = new JMenuItem();
        this.jMenuItem15 = new JMenuItem();
        this.jMenuItem19 = new JMenuItem();
        this.menuConfig = new JMenu();
        this.jMenuItem14 = new JMenuItem();
        this.jMenuItem16 = new JMenuItem();
        this.jMenu4 = new JMenu();
        this.jRadioButtonMenuItem1 = new JRadioButtonMenuItem();
        this.jRadioButtonMenuItem2 = new JRadioButtonMenuItem();
        this.jMenu5 = new JMenu();
        this.jRadioButtonMenuItem3 = new JRadioButtonMenuItem();
        this.jRadioButtonMenuItem4 = new JRadioButtonMenuItem();
        this.jRadioButtonMenuItem7 = new JRadioButtonMenuItem();
        this.jMenu6 = new JMenu();
        this.jRadioButtonMenuItem5 = new JRadioButtonMenuItem();
        this.jRadioButtonMenuItem6 = new JRadioButtonMenuItem();
        this.jMenuItem18 = new JMenuItem();
        this.jMenuItem17 = new JMenuItem();

        setDefaultCloseOperation(0);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                StartFrame.this.formWindowClosing(evt);
            }

        });
        this.jPanel1.setBorder(javax.swing.BorderFactory.createBevelBorder(0));
        this.jPanel1.setLayout(new java.awt.BorderLayout());

        this.guiUserList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                guiUserListMouseClicked(evt);
            }
        });
        this.guiUserList.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent evt) {
                guiUserListValueChanged(evt);
            }
        });
        this.jScrollPane1.setViewportView(this.guiUserList);

        this.jPanel1.add(this.jScrollPane1, "Center");

        this.labelTips.setText(" ");

        GroupLayout jPanel2Layout = new GroupLayout(this.jPanel2);
        this.jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup().addContainerGap()
                        .addComponent(this.labelTips, -2, 246, -2).addContainerGap()));

        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addContainerGap(14, 32767).addComponent(this.labelTips).addContainerGap()));

        this.jPanel1.add(this.jPanel2, "South");

        this.jMenuBar1.add(this.menuSystem);

        this.menuWindows.setText("窗口");

        this.jMenuItem8.setText("发送消息");
        this.jMenuItem8.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem8ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem8);

        this.jMenuItem9.setText("广播消息");
        this.menuWindows.add(this.jMenuItem9);

        this.jMenuItem10.setText("发送文件");
        this.jMenuItem10.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem10ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem10);
        this.menuWindows.add(this.jSeparator4);

        this.jMenuItem11.setText("管理好友");
        this.jMenuItem11.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem11ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem11);

        this.jMenuItem12.setText("管理黑名单");
        this.jMenuItem12.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem12ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem12);

        this.jMenuItem13.setText("日志");
        this.menuWindows.add(this.jMenuItem13);

        this.jMenuItem15.setText("攻击");
        this.jMenuItem15.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem15ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem15);

        this.jMenuItem19.setText("发送列表");
        this.jMenuItem19.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem19ActionPerformed(evt);
            }
        });
        this.menuWindows.add(this.jMenuItem19);

        this.jMenuBar1.add(this.menuWindows);

        this.menuConfig.setText("配置");

        this.jMenuItem14.setText("修改用户昵称");
        this.jMenuItem14.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem14ActionPerformed(evt);
            }
        });
        this.menuConfig.add(this.jMenuItem14);

        this.jMenuItem16.setText("修改主机昵称");
        this.jMenuItem16.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem16ActionPerformed(evt);
            }
        });
        this.menuConfig.add(this.jMenuItem16);

        this.jMenu4.setText("发送消息方式");

        this.buttonGroup1.add(this.jRadioButtonMenuItem1);
        this.jRadioButtonMenuItem1.setSelected(true);
        this.jRadioButtonMenuItem1.setText("Ctrl+Enter");
        this.jRadioButtonMenuItem1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem1ActionPerformed(evt);
            }
        });
        this.jMenu4.add(this.jRadioButtonMenuItem1);

        this.buttonGroup1.add(this.jRadioButtonMenuItem2);
        this.jRadioButtonMenuItem2.setText("Entel");
        this.jRadioButtonMenuItem2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem2ActionPerformed(evt);
            }
        });
        this.jMenu4.add(this.jRadioButtonMenuItem2);

        this.menuConfig.add(this.jMenu4);

        this.jMenu5.setText("编码类型");

        this.buttonGroup2.add(this.jRadioButtonMenuItem3);
        this.jRadioButtonMenuItem3.setSelected(true);
        this.jRadioButtonMenuItem3.setText("GB2312(推荐)");
        this.jRadioButtonMenuItem3.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem3ActionPerformed(evt);
            }
        });
        this.jMenu5.add(this.jRadioButtonMenuItem3);

        this.buttonGroup2.add(this.jRadioButtonMenuItem4);
        this.jRadioButtonMenuItem4.setText("UTF8");
        this.jRadioButtonMenuItem4.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem4ActionPerformed(evt);
            }
        });
        this.jMenu5.add(this.jRadioButtonMenuItem4);

        this.buttonGroup2.add(this.jRadioButtonMenuItem7);
        this.jRadioButtonMenuItem7.setText("shift_jis(鸟日本的)");
        this.jRadioButtonMenuItem7.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem7ActionPerformed(evt);
            }
        });
        this.jMenu5.add(this.jRadioButtonMenuItem7);

        this.menuConfig.add(this.jMenu5);

        this.jMenu6.setText("主题类型");

        this.buttonGroup3.add(this.jRadioButtonMenuItem5);
        this.jRadioButtonMenuItem5.setSelected(true);
        this.jRadioButtonMenuItem5.setText("JAVA");
        this.jRadioButtonMenuItem5.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem5ActionPerformed(evt);
            }
        });
        this.jMenu6.add(this.jRadioButtonMenuItem5);

        this.buttonGroup3.add(this.jRadioButtonMenuItem6);
        this.jRadioButtonMenuItem6.setText("OS_BASED");
        this.jRadioButtonMenuItem6.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jRadioButtonMenuItem6ActionPerformed(evt);
            }
        });
        this.jMenu6.add(this.jRadioButtonMenuItem6);

        this.menuConfig.add(this.jMenu6);

        this.jMenuItem18.setText("修改日志大小限制");
        this.jMenuItem18.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem18ActionPerformed(evt);
            }
        });
        this.menuConfig.add(this.jMenuItem18);

        this.jMenuItem17.setText("重置");
        this.jMenuItem17.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                StartFrame.this.jMenuItem17ActionPerformed(evt);
            }
        });
        this.menuConfig.add(this.jMenuItem17);

        this.jMenuBar1.add(this.menuConfig);

        setJMenuBar(this.jMenuBar1);

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(this.jPanel1, -1, 270, 32767));

        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(this.jPanel1, -1, 574, 32767));

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width - 278) / 2, (screenSize.height - 628) / 2, 278, 628);
    }

    private void formWindowClosing(java.awt.event.WindowEvent evt) {
        exit();
    }

    private void guiUserListValueChanged(ListSelectionEvent evt) {
        int selectLen = this.guiUserList.getSelectedIndices().length;
        if (selectLen == 1) {
            this.labelTips.setText("ip  ： " + this.guiUserList.getSelectedValue());
        }
        if (selectLen > 1) {
            this.labelTips.setText("已选择 " + selectLen + " 个用户！" + "点击菜单\"窗口-聊天\"进行群聊！");
        }
    }

    private void guiUserListMouseClicked(java.awt.event.MouseEvent evt) {
        if ((evt.getClickCount() == 2)
                && (!UtilityGui.newWindow(this.guiUserList.getSelectedValue(), null, null))) {
            this.labelTips.setText("窗口已打开");
        }
    }

    public void jMenuItem7ActionPerformed(ActionEvent evt) {
        exit();
    }

    public void jMenuItem6ActionPerformed(ActionEvent evt) {
        UtilityGui.showNotice(this,
                "兼容飞鸽的LanMsg" + GlobalVar.NEW_LINE + "兼容GB2312和UTF8编码" + GlobalVar.NEW_LINE + "兼容Windows和Linux平台"
                        + GlobalVar.NEW_LINE + "邱金武（qiujinwu456@gmail.com)" + GlobalVar.NEW_LINE + "为您服务!");
    }

    public void jMenuItem3ActionPerformed(ActionEvent evt) {
        StringBuffer sb = new StringBuffer();
        sb.append("共有 " + GlobalVar.ALL_IP_ADDRESS.size() + " 个ip" + GlobalVar.NEW_LINE);

        for (int i = 0; i < GlobalVar.ALL_IP_ADDRESS.size(); i++)
            sb.append((String) GlobalVar.ALL_IP_ADDRESS.get(i) + GlobalVar.NEW_LINE);
        UtilityGui.showNotice(this, sb.toString());
    }

    public void jMenuItem4ActionPerformed(ActionEvent evt) {
    }

    public void jMenuItem2ActionPerformed(ActionEvent evt) {
        int selectLen;

        if ((selectLen = this.guiUserList.getSelectedIndices().length) < 1) {
            this.labelTips.setText("请选择用户！");
            return;
        }
        if (selectLen == 1) {
            ipmsg.etc.User usr = GlobalVar.getUser((String) this.guiUserList.getSelectedValue());

            UtilityGui.showNotice(this, "昵称 ： " + usr.getName() + GlobalVar.NEW_LINE + "ip : " + usr.getIp()
                    + GlobalVar.NEW_LINE + "组别 ： " + usr.getGroup() + GlobalVar.NEW_LINE + "主机 ： " + usr.getHost());

        } else {
            Object[] o = this.guiUserList.getSelectedValues();

            StringBuffer sb = new StringBuffer();
            sb.append("昵称：ip：组别：主机" + GlobalVar.NEW_LINE);
            for (int i = 0; i < o.length; i++) {
                ipmsg.etc.User usr = GlobalVar.getUser((String) o[i]);
                sb.append(usr.getName() + " : " + usr.getIp() + " : " + usr.getGroup() + " : " + usr.getHost()
                        + GlobalVar.NEW_LINE);
            }

            UtilityGui.showNotice(this, sb.toString());
        }
    }

    public void jMenuItem1ActionPerformed(ActionEvent evt) {
        GlobalVar.clearUsers();
        refreshUsers();
        login();
    }

    private void jMenuItem14ActionPerformed(ActionEvent evt) {
        String str;
        if ((str = UtilityGui.getInputString(this, "输入昵称", "修改昵称", 50)) == null) {
            this.labelTips.setText("昵称修改被取消或失败！");
            return;
        }
        GlobalVar.USER_NAME = str;
        setTitle("飞鸽(LanMsg)-" + GlobalVar.USER_NAME);
        broadcastPriInfo();
    }

    private void jRadioButtonMenuItem1ActionPerformed(ActionEvent evt) {
        GlobalVar.SEND_MSG_STYLE = '\000';
    }

    private void jRadioButtonMenuItem2ActionPerformed(ActionEvent evt) {
        GlobalVar.SEND_MSG_STYLE = '\001';
    }

    private void jRadioButtonMenuItem3ActionPerformed(ActionEvent evt) {
        GlobalVar.CHARACTER_ENCODING = "GB2312";
    }

    private void jRadioButtonMenuItem4ActionPerformed(ActionEvent evt) {
        GlobalVar.CHARACTER_ENCODING = "UTF8";
    }

    private void jRadioButtonMenuItem5ActionPerformed(ActionEvent evt) {
        GlobalVar.THEME = '\000';
        this.labelTips.setText("主题生效需要重启程序！");
    }

    private void jRadioButtonMenuItem6ActionPerformed(ActionEvent evt) {
        GlobalVar.THEME = '\001';
        this.labelTips.setText("主题生效需要重启程序！");
    }

    private void jMenuItem17ActionPerformed(ActionEvent evt) {
        if (!UtilityGui.confirm(this, "重置设置", "您确定要重置吗？")) {
            return;
        }
        GlobalVar.initDefault();
        broadcastPriInfo();
        UtilityGui.showNotice(this, "主题生效需要重启程序！");
        freshGui(false);
    }

    private void jMenuItem18ActionPerformed(ActionEvent evt) {
        this.labelTips.setText("当前日志最大长度限制为" + GlobalVar.LOG_MAX_LEN + " (KB)");
        int maxSize;
        if ((maxSize = ipmsg.etc.UtilityGlobal
                .getNumber(UtilityGui.getInputString(this, "输入长度（请输入数字,单位：K）", "修改日志最大长度限制", 10))) < 0) {

            this.labelTips.setText("修改日志最大长度限制被取消或失败！");
            return;
        }
        GlobalVar.LOG_MAX_LEN = maxSize;
        this.labelTips.setText("修改日志最大长度限制至" + maxSize + " (KB)");
    }

    private void jMenuItem16ActionPerformed(ActionEvent evt) {
        String str = UtilityGui.getInputString(this, "输入主机名", "修改主机名称", 50);
        if (str == null) {
            this.labelTips.setText("主机名修改被取消或失败！");
            return;
        }
        GlobalVar.HOST_NAME = str;
        broadcastPriInfo();
    }

    private void jMenuItem8ActionPerformed(ActionEvent evt) {
        int selectLen;
        if ((selectLen = this.guiUserList.getSelectedIndices().length) < 1) {
            this.labelTips.setText("请选择用户！");
            return;
        }
        if (selectLen == 1) {
            if (!UtilityGui.newWindow(this.guiUserList.getSelectedValue(), null, null)) {
                this.labelTips.setText("窗口已经打开！");
            }
        } else {
            Object[] o = this.guiUserList.getSelectedValues();
            java.util.ArrayList<String> tmp = new java.util.ArrayList();
            for (int i = 0; i < o.length; i++) {
                tmp.add((String) o[i]);
            }
            new ipmsg.gui.MsgWindow(tmp).setVisible(true);
        }
    }

    private void jRadioButtonMenuItem7ActionPerformed(ActionEvent evt) {
        GlobalVar.CHARACTER_ENCODING = "SHIFT_JIS";
    }

    private void jMenuItem11ActionPerformed(ActionEvent evt) {
        if (!GlobalVar.WINDOW_REG.contains("addedUser"))
            new ipmsg.gui.AddedUsers().setVisible(true);
        else {
            this.labelTips.setText("窗口已打开");
        }
    }

    private void jMenuItem12ActionPerformed(ActionEvent evt) {
        if (!GlobalVar.WINDOW_REG.contains("blacklist"))
            new ipmsg.gui.Blacklist().setVisible(true);
        else {
            this.labelTips.setText("窗口已打开");
        }
    }

    public void jMenuItem5ActionPerformed(ActionEvent evt) {
        if (this.guiUserList.getSelectedIndices().length < 1) {
            this.labelTips.setText("请选择用户！");
            return;
        }
        boolean change = false;
        Object[] o = this.guiUserList.getSelectedValues();
        for (int i = 0; i < o.length; i++)
            if ((!GlobalVar.BLACK_LIST.contains((String) o[i]))
                    && (!GlobalVar.ALL_IP_ADDRESS.contains((String) o[i]))) {
                GlobalVar.BLACK_LIST.add((String) o[i]);
                GlobalVar.delUser((String) o[i]);
                change = true;
            }
        if (change) {
            refreshUsers();
        }
    }

    private void jMenuItem15ActionPerformed(ActionEvent evt) {
        int selectLen;
        if ((selectLen = this.guiUserList.getSelectedIndices().length) < 1) {
            this.labelTips.setText("请选择用户！");
            return;
        }
        // if (selectLen == 1) {
        // if (!GlobalVar.WINDOW_REG.contains("attack")) {
        // new ipmsg.gui.Attack((String)
        // this.guiUserList.getSelectedValue()).setVisible(true);
        // } else {
        // this.labelTips.setText("群攻危险");
        // }//end if
        // } else {
        // this.labelTips.setText("群攻危险！");
        // }//end if
    }

    private void jMenuItem10ActionPerformed(ActionEvent evt) {
        int selectLen;
        if ((selectLen = this.guiUserList.getSelectedIndices().length) < 1) {
            this.labelTips.setText("请选择用户！");
            return;
        }
        if (selectLen == 1) {
            new ipmsg.gui.SendFiles((String) this.guiUserList.getSelectedValue()).setVisible(true);
        } else {
            this.labelTips.setText("不支持同时发送给多个用户！");
        }
    }

    private void jMenuItem19ActionPerformed(ActionEvent evt) {
        new ipmsg.gui.ReadyToSendFiles().setVisible(true);
    }

    private JMenuItem jMenuItem19;

    private JMenuItem jMenuItem8;

    private JMenuItem jMenuItem9;

    private javax.swing.JPanel jPanel1;

    private javax.swing.JPanel jPanel2;

    private JRadioButtonMenuItem jRadioButtonMenuItem1;

    private JRadioButtonMenuItem jRadioButtonMenuItem2;

    private JRadioButtonMenuItem jRadioButtonMenuItem3;

    private JRadioButtonMenuItem jRadioButtonMenuItem4;

    private JRadioButtonMenuItem jRadioButtonMenuItem5;

    private JRadioButtonMenuItem jRadioButtonMenuItem6;

    private JRadioButtonMenuItem jRadioButtonMenuItem7;

    private javax.swing.JScrollPane jScrollPane1;

    private javax.swing.JSeparator jSeparator4;

    private void exit() {
        if (!UtilityGui.confirm(this, "退出程序", "您确定要退出吗？")) {
            return;
        }
        ipmsg.etc.Command tmpCom = new ipmsg.etc.Command(2);
        tmpCom.setAdditional(GlobalVar.USER_NAME);
        ipmsg.network.UtilityNet.broadcastUdpPacket(tmpCom);
        ipmsg.file.Configuration.writeConfig();
        ipmsg.file.ConfigBlacklist.writeConfig();
        ipmsg.file.ConfigAddedUsers.writeConfig();
        System.exit(0);
    }

    private void login() {
        ipmsg.etc.Command tmp = new ipmsg.etc.Command(1);
        tmp.setAdditional(GlobalVar.USER_NAME + '\000' + "LanMsg");
        ipmsg.network.UtilityNet.broadcastUdpPacket(tmp);
    }

    private void freshGui(boolean boot) {
        setTitle("飞鸽(LanMsg)-" + GlobalVar.USER_NAME);
        if (GlobalVar.SEND_MSG_STYLE == 0)
            this.jRadioButtonMenuItem1.setSelected(true);
        else {
            this.jRadioButtonMenuItem2.setSelected(true);
        }
        if (GlobalVar.CHARACTER_ENCODING.equalsIgnoreCase("GB2312")) {
            this.jRadioButtonMenuItem3.setSelected(true);
        } else if (GlobalVar.CHARACTER_ENCODING.equalsIgnoreCase("UTF8"))
            this.jRadioButtonMenuItem4.setSelected(true);
        else {
            this.jRadioButtonMenuItem7.setSelected(true);
        }

        if (GlobalVar.THEME == 0)
            this.jRadioButtonMenuItem5.setSelected(true);
        else {
            this.jRadioButtonMenuItem6.setSelected(true);
        }
        if (!boot)
            broadcastPriInfo();
    }

    private void broadcastPriInfo() {
        ipmsg.etc.Command com = new ipmsg.etc.Command(4);
        com.setAdditional(GlobalVar.USER_NAME + '\000' + "LanMsg");
        ipmsg.network.UtilityNet.broadcastUdpPacket(com);
    }

    private void checkVersion() {
        if (System.getProperty("java.version").charAt(2) < '6') {
            UtilityGui.showNotice(this, "jdk 版本太低！");
            System.exit(1);
        }

        if (!ipmsg.network.UtilityNet.checkPort()) {
            UtilityGui.showNotice(this, "程序可能已经被打开一次！");
            System.exit(1);
        }
    }

    private void checkCharset() {
        try {
            new String("qiujinwu").getBytes(GlobalVar.CHARACTER_ENCODING);
        } catch (java.io.UnsupportedEncodingException ex) {
            UtilityGui.showNotice(this,
                    "您的jdk似乎不支持目前的编码 ：\"" + GlobalVar.CHARACTER_ENCODING + "\"" + GlobalVar.NEW_LINE
                            + "您可以通过将\"charset.jar\"拷贝到" + GlobalVar.NEW_LINE + "目录\"" + System.getProperty("java.home")
                            + GlobalVar.FILE_PATH_DELIMITER + "lib\"解决该问题");

            System.exit(-1);
        }
    }

    private void initTheme() {
        if (GlobalVar.THEME == '\001') {
            try {
                javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName());
            } catch (Exception e) {
                this.labelTips.setText("初始化主题失败！");
            }
        }
    }

    public void refreshUsers() {
        listCellRenderer render = new listCellRenderer();
        this.guiUserList.setListData(GlobalVar.getUserList());
        this.guiUserList.setCellRenderer(render);
    }

    static class listCellRenderer extends javax.swing.DefaultListCellRenderer {
        public java.awt.Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected,
                                                               boolean cellHasFocus) {
            super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
            if ((value instanceof String)) {
                String userName = GlobalVar.getUser((String) value).getName();

                if (userName != null) {
                    setText(userName);
                }
            }
            return this;
        }
    }
}
